package com.baizhi.controller;

import com.baizhi.annotation.LogAnnotation;
import com.baizhi.annotation.LogAnnotation2;
import com.baizhi.common.Constant;
import com.baizhi.entity.CmfzBanner;
import com.baizhi.service.CmfzBannerService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("cmfzBanner")
@Slf4j
public class CmfzBannerController {
    @Autowired
    private CmfzBannerService cmfzBannerService;
    @RequestMapping("show")
    @LogAnnotation(content = "查询所有轮播图信息",type = "查询")
    public Map showBanner(Integer page,Integer limit){
        Map map = new HashMap();
        Page<CmfzBanner> bannerPage = cmfzBannerService.selectPage(page, limit);
        map.put("code",0);
        map.put("msg","");
        map.put("count",bannerPage.getTotal());
        map.put("data",bannerPage.getRecords());
        return map;
    }

    @RequestMapping("upload")
    @LogAnnotation(content = "轮播图上传",type = "上传")
    public Map upload(MultipartFile file){
        Map map = new HashMap();
        String fileName = file.getOriginalFilename();
        String newFileName = UUID.randomUUID().toString()+fileName;
        try {
            file.transferTo(new File(Constant.BANNER_IMG_LOCATION,newFileName));
            map.put("fileName",newFileName);
            map.put("flag",true);
            log.info("上传后的轮播图名字："+newFileName);
        } catch (IOException e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("add")
    @LogAnnotation(content = "添加轮播图信息",type = "添加")
    public Map addBanner(CmfzBanner cmfzBanner){
        Map map = new HashMap();
        try {
            cmfzBannerService.insertCmfzBanner(cmfzBanner);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("update")
    @LogAnnotation(content = "更新轮播图信息",type = "修改")
    public Map updateBanner(CmfzBanner cmfzBanner){
        log.info("修改方法的参数："+cmfzBanner);
        Map map = new HashMap();
        try {
            cmfzBannerService.updateCmfzBanner(cmfzBanner);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("remove")
    @LogAnnotation(content = "删除轮播图信息",type = "删除")
    public Map removeBanner(Integer bannerId){
        Map map = new HashMap();
        try {
            cmfzBannerService.deleteCmfzBanner(bannerId);
            map.put("flag",true);
        } catch (Exception e) {
            map.put("flag",false);
        }
        return map;
    }
    @RequestMapping("getOne")
    @LogAnnotation(content = "查询一个轮播图信息",type = "查询")
    public CmfzBanner getOneBanner(Integer bannerId){
        CmfzBanner cmfzBanner = cmfzBannerService.selectById(bannerId);
        log.info("要修改的对象信息："+cmfzBanner);
        return cmfzBanner;
    }
}
